<?php

abstract class MCActiveRecord extends CActiveRecord
{
	public $current_conn_type = DB_CONN;
	public $current_conn_idx = false;
	
	public static function model($className = '')
	{
	    $className .= 'Model';
		//PHP 5 >= 5.3.0
		if (function_exists('get_called_class') && empty($className))
		{
			$className = get_called_class();
		}
		return parent::model($className);
	}
	
	public function tableName()
    {
        return '{{'.$this->_table.'}}';
    }
    
    public function primaryKey()
    {
    	return $this->_id;
    }
	
    /**
     * 分页方法
     * @param string $condition 附加条件
     * @param number $pageSize 每分显示行数
     * @param mixed $with 关联查询
     * @param string $pageVar	分页标签
     * @return array
     */
    public function pager($condition = '', $pageSize = 20, $with = false, $pageVar = 'p')
    {
    	$pages = '';
    	$models = '';
    	$criteria = new CDbCriteria($condition);
    	$count = $this->count($criteria);
    	if ($count > 0)
    	{
    		$pages = new CPagination($count);
    		$pages->pageVar = $pageVar;
    		$pages->pageSize = $pageSize;
    		//$pages->route = '/role/index';
    		$pages->applyLimit($criteria);
    		if ($with)
    		{
    			$this->with($with);
    		}
    		$models = $this->findAll($criteria);
    	}
    	return array('pages' => $pages, 'res' => $models);
    }
    
    public final function setConnType($t)
    {
    	$this->current_conn_type = $t;
    }
    
    public final function getConnType()
    {
    	return $this->current_conn_type;
    }
    
    public final function setConnIdx($idx)
    {
    	$this->current_conn_idx = $idx;
    }
    
    public final function getConnIdx()
    {
    	 return $this->current_conn_idx;
    }
    
    public function getDbConnection($t = false, $idx = false)
    {
    	if ($t !== false)
    		$this->setConnType($t);
    	
    	if ($idx !== false)
    		$this->setConnIdx($idx);
    	
    	return MCDbConnection::getDbConnection($this->getConnType(), $this->getConnIdx());
    }
    
    public function getCommandBuilder($t = false, $idx = false)
    {
    	if ($t !== false)
    		$this->setConnType($t);
    	 
    	if ($idx !== false)
    		$this->setConnIdx($idx);
    	
    	return $this->getDbConnection($this->getConnType(), $this->getConnIdx())->getSchema()->getCommandBuilder();
    }
    
    public function deleteAll($condition='', $params=array())
    {
    	if($this->beforeDelete())
    	{
    		return parent::deleteAll($condition, $params);
    	}
    }
    
    public function deleteAllByAttributes($attributes, $condition='', $params=array())
    {
    	if($this->beforeDelete())
    	{
    		return parent::deleteAllByAttributes($attributes, $condition, $params);
    	}
    }
    
    public function deleteByPk($pk, $condition='', $params=array())
    {
    	if($this->beforeDelete())
    	{
    		return parent::deleteByPk($pk, $condition, $params);
    	}
    }
    
    public function updateAll($attributes,$condition='',$params=array())
    {
    	if ($this->beforeSave())
    	{
    		return parent::updateAll($attributes, $condition, $params);
    	}
    }
    
    public function updateByPk($pk, $attributes, $condition='', $params=array())
    {
    	if ($this->beforeSave())
    	{
    		return parent::updateByPk($pk, $attributes, $condition, $params);
    	}
    }
    
    public function updateCounters($counters,$condition='',$params=array())
    {
    	if ($this->beforeSave())
    	{
    		return parent::updateCounters($counters, $condition, $params);
    	}
    }
    
    public function saveCounters($counters)
    {
    	if ($this->beforeSave())
    	{
    		return parent::saveCounters($counters);
    	}
    }
    
    public function saveAttributes($attributes)
    {
    	if ($this->beforeSave())
    	{
    		return parent::saveAttributes($attributes);
    	}
    }
    
    public function find($condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::find($condition, $params);
    }
    
    public function findAll($condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::findAll($condition, $params);
    }
    
    public function findByPk($pk,$condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::findByPk($pk, $condition, $params);
    }
    
    public function findAllByPk($pk,$condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::findAllByPk($pk,$condition,$params);
    }
    
    public function findByAttributes($attributes,$condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::findByAttributes($attributes, $condition, $params);
    }
    
    public function findAllByAttributes($attributes,$condition='',$params=array())
    {
    	$this->beforeFind();
    	return parent::findAllByAttributes($attributes, $condition, $params);
    }
    
    protected function beforeFind()
    {
    	$this->setConnType(DB_READ);
    	return parent::beforeFind();
    }
    
    protected function beforeCount()
    {
    	$this->setConnType(DB_READ);
    	return parent::beforeCount();
    }
    
    protected function beforeSave()
    {
    	$this->setConnType(DB_WRITE);
    	return parent::beforeSave();
    }
    
    protected function beforeDelete()
    {
    	$this->setConnType(DB_WRITE);
    	return parent::beforeDelete();
    }
}